package de.dfki.asr.genesis2.sms.oauth;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.neurologic.oauth.service.impl.OAuth1Service;

/**
 * Servlet implementation class TwitterOAuth
 */
@WebServlet("/fbauth")
public class FacebookOAuth extends OAuthServlet {
	private static final long serialVersionUID = 1L;
	
    protected static final String FACEBOOK_ACCESS_TOKEN_SESSION = "FACEBOOK_ACCESS_TOKEN_SESSION";
    //protected static final String redirect_uri = "http://localhost:8080/sms/FacebookOAuthService";
    protected static final String redirect_uri = "http://localhost:8080/sms/FacebookOAuthService";
    protected static final String client_id = "400240100000830";
	protected static final String client_secret = "7c46a4fd701e7b0b122e3885c41f8373";
	protected static final String callback_url = "http://localhost:8080/Test/TestFacebookServlet";
	protected static final String accessToken_url = "https://graph.facebook.com/oauth/access_token?";
	protected static final String requestCode_url = "https://www.facebook.com/dialog/oauth/?";
	protected static final String user_scope = "user_about_me,user_activities,user_checkins,user_events,user_hometown,user_likes,user_notes,user_photos,user_status,user_videos,user_work_history,email";
	protected static final String friends_scope = "friends_about_me,friends_activities,friends_checkins,friends_events,friends_hometown,friends_likes,friends_notes,friends_photos,friends_status,friends_videos,friends_work_history";
	protected static final String extended_scope = "read_friendlists,read_mailbox,read_stream";
	protected static final String openGraph_scope = "user_actions.music,user_actions.news,user_actions.video";
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FacebookOAuth() {
        super();
        // TODO Auto-generated constructor stub
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		++stateIncrementer;
		
		//String ip = request.getRemoteAddr();
		
		String url = requestCode_url + 
				 "client_id=" + client_id +
				 "&redirect_uri=" + redirect_uri +
				 "&state=" + stateIncrementer + 
				 "&response_type=code" +
				 "&scope=" + user_scope + "," + friends_scope + "," + extended_scope + "," +openGraph_scope;
		
		
		response.getWriter().write("redirect_uri=" + url);			 
		
		response.sendRedirect(url);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
